#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "Log.h"

#define MAX_LOG_LENGTH 1024


Logger::Logger()
{
    fd = -1;
}

Logger::~Logger()
{
    if(fd >=0) {
        close(fd);
    }
    fd = -1;
}

Logger::Logger(const char *logFilePre, int m, int lev)
{
    fd = -1;
    logLevel = lev;
    memset(filePrefix, 0, sizeof(filePrefix));
    strncpy(filePrefix, logFilePre, sizeof(filePrefix)-1);
    mode = m;
}

//模块名称、级别、格式
bool Logger::Log(const char *modName, int level, const char *fmt, ...) 
{
    int len;
    va_list ap;//参数列表
    char *line;
    char filename[128];
    static struct tm otm;
    struct tm tm;
    time_t t;
    static const char *levelStr[] = {"DEBUG", "INFO", "WARN", "ERROR"};//字符串常量数组

    if(level < logLevel)
    {
        return true;
    }

    t = time(NULL);
    localtime_r(&t, &tm);


    switch(mode)
    {
    case DAYLY_LOG:
        if(tm.tm_mday!=otm.tm_mday)
        {
            sprintf(filename, "./logs/%s_%04d%02d%02d.log",  this->filePrefix, tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
            ::close(fd);
            fd=-1;
        }
        break;

    case MONTHLY_LOG:
        if(tm.tm_mon!=otm.tm_mon)
        {
           sprintf(filename, "./logs/%s_%4d%02d.log",this->filePrefix,tm.tm_year+1900,tm.tm_mon+1);
           ::close(fd);
           fd=-1;
        }
        break;
    case YEARLY_LOG:
        if(tm.tm_year!=otm.tm_year)
        {
            sprintf(filename, "./logs/%s_%4d.log",this->filePrefix,tm.tm_year+1900);
            ::close(fd);
            fd=-1;
        }
        break;
    default:
        sprintf(filename, "./logs/%s.log", this->filePrefix);
        break;
    }

    //根据日志记录模式，判断是否已是新的一天或新的一个月，若是，则需要根据情况关闭文件再创建新的文件

    //先判断文件是否已打开，未打开则打开文件
    if(fd  == -1) {
        //根据模式确定打开的文件名格式，追加模式打开
        if((fd = open(filename, O_CREAT|O_APPEND|O_WRONLY, 0666)) < 0) {
            perror("open");
            return false;
        }
    }


    line = (char *)malloc(MAX_LOG_LENGTH+1);
    if(line == NULL) {
        return false;
    }

    len = snprintf(line, MAX_LOG_LENGTH,"[%04d-%02d-%02d %02d:%2d:%2d][%s][%s]:",
                   tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour,
                   tm.tm_min, tm.tm_sec, modName, levelStr[level]);


    va_start(ap, fmt);
    len = vsnprintf(line+len, MAX_LOG_LENGTH-len, fmt, ap);
    va_end( ap );


    write(fd, line, strlen(line));

    free(line);


    return true;
}

